#!/usr/bin/evn python
# coding:utf-8
import os
import numpy as np

try:
    import xml.etree.cElementTree as ET
except ImportError:
    import xml.etree.ElementTree as ET
import sys

how_many_delete=0
total_frame = 0
anno_path = "/home/sean/data/ILSVRC/Annotations/VID/train/"
img_root = "/home/sean/data/ILSVRC/Data/VID/train_remove_noobject/"
log_file = '/home/sean/data/ILSVRC/ImageSets/VID/train_no_object.txt'
list_dirs = os.walk(anno_path, topdown=True)

with open(log_file, 'w') as log_file:
    for root, dirs, files in list_dirs:

        for f in files:
            has_object = True
            file_path= os.path.join(root, f)
            tree = ET.parse(file_path)
            root_node = tree.getroot()
            object = root_node.find('object')
            total_frame += 1
            if not object:
                video_path = root.split('/')[8:]
                img_path = os.path.join(img_root, video_path[0], video_path[1], f.split('.')[0]+'.JPEG')
                os.remove(img_path)
                log_file.write(os.path.join(video_path[0], video_path[1], f.split('.')[0])+'\n')
                how_many_delete += 1

print(total_frame, how_many_delete)

